// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ui;

import zombie.Lua.LuaManager;
import zombie.core.Color;
import zombie.core.textures.Texture;

public final class DialogButton extends UIElement {
    public boolean clicked = false;
    public UIElement MessageTarget;
    public boolean mouseOver = false;
    public String name;
    public String text;
    Texture downLeft;
    Texture downMid;
    Texture downRight;
    float origX;
    Texture upLeft;
    Texture upMid;
    Texture upRight;
    private UIEventHandler MessageTarget2 = null;

    public DialogButton(UIElement messages, float x, float y, String _text, String _name) {
        this.x = (double)x;
        this.y = (double)y;
        this.origX = x;
        this.MessageTarget = messages;
        this.upLeft = Texture.getSharedTexture("ButtonL_Up");
        this.upMid = Texture.getSharedTexture("ButtonM_Up");
        this.upRight = Texture.getSharedTexture("ButtonR_Up");
        this.downLeft = Texture.getSharedTexture("ButtonL_Down");
        this.downMid = Texture.getSharedTexture("ButtonM_Down");
        this.downRight = Texture.getSharedTexture("ButtonR_Down");
        this.name = _name;
        this.text = _text;
        this.width = (float)TextManager.instance.MeasureStringX(UIFont.Small, _text);
        this.width += 8.0F;
        if (this.width < 40.0F) {
            this.width = 40.0F;
        }

        this.height = (float)this.downMid.getHeight();
        this.x = this.x - (double)(this.width / 2.0F);
    }

    public DialogButton(UIEventHandler messages, int x, int y, String _text, String _name) {
        this.x = (double)x;
        this.y = (double)y;
        this.origX = (float)x;
        this.MessageTarget2 = messages;
        this.upLeft = Texture.getSharedTexture("ButtonL_Up");
        this.upMid = Texture.getSharedTexture("ButtonM_Up");
        this.upRight = Texture.getSharedTexture("ButtonR_Up");
        this.downLeft = Texture.getSharedTexture("ButtonL_Down");
        this.downMid = Texture.getSharedTexture("ButtonM_Down");
        this.downRight = Texture.getSharedTexture("ButtonR_Down");
        this.name = _name;
        this.text = _text;
        this.width = (float)TextManager.instance.MeasureStringX(UIFont.Small, _text);
        this.width += 8.0F;
        if (this.width < 40.0F) {
            this.width = 40.0F;
        }

        this.height = (float)this.downMid.getHeight();
        this.x = this.x - (double)(this.width / 2.0F);
    }

    @Override
    public Boolean onMouseDown(double x, double y) {
        if (!this.isVisible()) {
            return false;
        } else {
            if (this.getTable() != null && this.getTable().rawget("onMouseDown") != null) {
                Object[] object = LuaManager.caller.pcall(LuaManager.thread, this.getTable().rawget("onMouseDown"), new Object[]{this.table, x, y});
            }

            this.clicked = true;
            return Boolean.TRUE;
        }
    }

    @Override
    public Boolean onMouseMove(double dx, double dy) {
        this.mouseOver = true;
        if (this.getTable() != null && this.getTable().rawget("onMouseMove") != null) {
            Object[] object = LuaManager.caller.pcall(LuaManager.thread, this.getTable().rawget("onMouseMove"), new Object[]{this.table, dx, dy});
        }

        return Boolean.TRUE;
    }

    @Override
    public void onMouseMoveOutside(double dx, double dy) {
        this.clicked = false;
        if (this.getTable() != null && this.getTable().rawget("onMouseMoveOutside") != null) {
            Object[] object = LuaManager.caller.pcall(LuaManager.thread, this.getTable().rawget("onMouseMoveOutside"), new Object[]{this.table, dx, dy});
        }

        this.mouseOver = false;
    }

    @Override
    public Boolean onMouseUp(double x, double y) {
        if (this.getTable() != null && this.getTable().rawget("onMouseUp") != null) {
            Object[] object = LuaManager.caller.pcall(LuaManager.thread, this.getTable().rawget("onMouseUp"), new Object[]{this.table, x, y});
        }

        if (this.clicked) {
            if (this.MessageTarget2 != null) {
                this.MessageTarget2.Selected(this.name, 0, 0);
            } else if (this.MessageTarget != null) {
                this.MessageTarget.ButtonClicked(this.name);
            }
        }

        this.clicked = false;
        return Boolean.TRUE;
    }

    @Override
    public void render() {
        if (this.isVisible()) {
            boolean _boolean = false;
            if (this.clicked) {
                this.DrawTexture(this.downLeft, 0.0, 0.0, 1.0);
                this.DrawTextureScaledCol(
                    this.downMid,
                    (double)this.downLeft.getWidth(),
                    0.0,
                    (double)((int)(this.getWidth() - (double)(this.downLeft.getWidth() * 2))),
                    (double)this.downLeft.getHeight(),
                    new Color(255, 255, 255, 255)
                );
                this.DrawTexture(this.downRight, (double)((int)(this.getWidth() - (double)this.downRight.getWidth())), 0.0, 1.0);
                this.DrawTextCentre(this.text, this.getWidth() / 2.0, 1.0, 1.0, 1.0, 1.0, 1.0);
            } else {
                this.DrawTexture(this.upLeft, 0.0, 0.0, 1.0);
                this.DrawTextureScaledCol(
                    this.upMid,
                    (double)this.downLeft.getWidth(),
                    0.0,
                    (double)((int)(this.getWidth() - (double)(this.downLeft.getWidth() * 2))),
                    (double)this.downLeft.getHeight(),
                    new Color(255, 255, 255, 255)
                );
                this.DrawTexture(this.upRight, (double)((int)(this.getWidth() - (double)this.downRight.getWidth())), 0.0, 1.0);
                this.DrawTextCentre(this.text, this.getWidth() / 2.0, 0.0, 1.0, 1.0, 1.0, 1.0);
            }

            super.render();
        }
    }
}
